#include "kdpch.h"
#include "OrthographicCameraController.h"

#include "KuaDu/Core/Input.h"
#include "KuaDu/Core/KeyCodes.h"

namespace KD {

	OrthographicCameraController::OrthographicCameraController(float aspectRatio, bool rotation)
		: m_Camera(-m_AspectRatio * m_ZoomLevel, m_AspectRatio* m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel),
		m_AspectRatio(aspectRatio), m_Rotation(rotation)
	{
	}

	void OrthographicCameraController::OnUpdate(TimeStep ts)
	{
		// 移动相机
		if (Input::IsKeyPressed(KEY::Left) || Input::IsKeyPressed(KEY::A))
		{
			m_CameraPosition.x -= cos(glm::radians(m_CameraRotation) * m_CameraTranslationSpeek * ts);
			m_CameraPosition.y -= sin(glm::radians(m_CameraRotation) * m_CameraTranslationSpeek * ts);
		}
		else if (KD::Input::IsKeyPressed(KEY::Right) || KD::Input::IsKeyPressed(KEY::D))
		{
			m_CameraPosition.x += cos(glm::radians(m_CameraRotation) * m_CameraTranslationSpeek * ts);
			m_CameraPosition.y += sin(glm::radians(m_CameraRotation) * m_CameraTranslationSpeek * ts);
		}

		if (Input::IsKeyPressed(KEY::Down) || Input::IsKeyPressed(KEY::S))
		{
			m_CameraPosition.x -= -sin(glm::radians(m_CameraRotation) * m_CameraTranslationSpeek * ts);
			m_CameraPosition.y -= cos(glm::radians(m_CameraRotation) * m_CameraTranslationSpeek * ts);
		}
		else if (Input::IsKeyPressed(KEY::Up) || Input::IsKeyPressed(KEY::W))
		{
			m_CameraPosition.x += -sin(glm::radians(m_CameraRotation) * m_CameraTranslationSpeek * ts);
			m_CameraPosition.y += cos(glm::radians(m_CameraRotation) * m_CameraTranslationSpeek * ts);
		}

		if (m_Rotation)
		{
			// 相机角度
			if (Input::IsKeyPressed(KEY::Q))
				m_CameraRotation += m_CameraRotationSpeek * ts;
			else if (Input::IsKeyPressed(KEY::E))
				m_CameraRotation -= m_CameraRotationSpeek * ts;

			if (m_CameraRotation > 180.0f)
				m_CameraRotation -= 360.0f;
			else if (m_CameraRotation <= -180.0f)
				m_CameraRotation += 360.0f;

			m_Camera.SetRotation(m_CameraRotation);
		}

		m_Camera.SetPosition(m_CameraPosition);
		m_CameraTranslationSpeek = m_ZoomLevel;
	}

	void OrthographicCameraController::OnEvent(Event& e)
	{
		EventDispatcher dispatcher(e);
		dispatcher.Dispatch<MouseScrolledEvent>(KD_BIND_EVENT_FN(OrthographicCameraController::OnMouseScrolled));
		dispatcher.Dispatch<WindowResizeEvent>(KD_BIND_EVENT_FN(OrthographicCameraController::OnWindowResize));
	}

	void OrthographicCameraController::OnResize(float width, float height)
	{
		m_AspectRatio = width / height;
		RecalculateView();
	}

	void OrthographicCameraController::RecalculateView()
	{
		m_Camera.SetProjection(-m_AspectRatio * m_ZoomLevel, m_AspectRatio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel);
	}

	bool OrthographicCameraController::OnMouseScrolled(MouseScrolledEvent& e)
	{
		m_ZoomLevel -= (float)(e.GetYOffset() * 0.2f);
		m_ZoomLevel = std::min(m_ZoomLevel, 5.0f);
		m_ZoomLevel = std::max(m_ZoomLevel, 0.2f);
		KD_CORE_WARN("{0}", m_ZoomLevel);
		RecalculateView();
		return false;
	}

	bool OrthographicCameraController::OnWindowResize(WindowResizeEvent& e)
	{
		OnResize((float)e.GetWidth(), (float)e.GetHeight());
		return false;
	}

}